package com.example.lottery.common.pojo;


import com.example.lottery.common.errorcode.ErrorCode;
import com.example.lottery.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.data = data;
        commonResult.message = " ";
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"状态码符合错误的状态码");
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = code;
        commonResult.message = message;
        return commonResult;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(),errorCode.getMessage());
    }

}
